//
// JTVariableToken
//
// Copyright (C) jextra.net.
//
//  This file is part of the JavaTemplar Library.
//
//  The JavaTemplar Library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The JavaTemplar Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the JavaTemplar Library; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.javatemplar.token;

import java.io.*;
import net.jextra.javatemplar.*;

/**
 * <p>
 * Represents a variable reference in the file.
 * </p><p>
 * This is define in a template file by a dollar sign, a left brace, a name, and a right brace:
 * <pre>
 *     ${myVariable}
 * </pre>
 * The variable name is a sequence of one or more characters that are a letter, digit, or symbol 
 * other than right brace. It should not contain spaces.
 */
public class JTVariableToken implements JTToken
{
    // ============================================================
    // Fields
    // ============================================================

    private String _name;
    private String _value;
    private boolean _strict;

    // ============================================================
    // Constructors
    // ============================================================

    public JTVariableToken( String name )
    {
    	this( name, null, false );
    }

    public JTVariableToken( String name, String value )
    {
    	this( name, value, false );
    }
    
    public JTVariableToken( String name, String value, boolean isStrict )
    {
    	_name = name;
    	_value = value;
    	_strict = isStrict;
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    @Override
    public JTToken clone()
    {
        return new JTVariableToken( _name, _value, _strict );
    }

    public String getName()
    {
        return _name;
    }

    public String getValue()
    {
        return _value;
    }

    public void setValue( String value )
    {
        _value = value;
    }
    
    public boolean isStrict()
    {
    	return _strict;
    }
    
    public void setStrict( boolean isStrict )
    {
    	_strict = isStrict;
    }

    public void write( Writer out )
        throws Exception
    {
        if ( _value == null && _strict )
        {
            throw new JavaTemplarException( "Variable '" + _name + "' is unassigned." );
        }
        else if ( _value == null && !_strict )
        {
        	out.write( "${" + _name + "}" );
        	return;
        }

        out.write( _value );
    }
}

